package jvm;

import java.io.IOException;
import java.io.InputStream;

public class ClassInstanceAndClassLoader {
	public static void main(String[] args) throws Exception {
		ClassLoader myClassLoader = new ClassLoader() {
			@Override
			public Class<?> loadClass(String name) throws ClassNotFoundException {
				try {
					System.out.println(String.format("loading class %s", name));
					InputStream resource = getClass().getResourceAsStream(name.substring(name.lastIndexOf(".") + 1) + ".class");
					if (resource == null) {
						System.out.println("no class file for " + name);
						return super.loadClass(name);
					}
					byte[] bytes = new byte[resource.available()];
					resource.read(bytes);
					return defineClass(name, bytes, 0, bytes.length);
				} catch (IOException e) {
					throw new ClassNotFoundException();
				}
			}
		};

		Object myObj = myClassLoader.loadClass(ClassInstanceAndClassLoader.class.getName()).newInstance();

		System.out.println(myObj instanceof ClassInstanceAndClassLoader);       // false
	}
}